package com.bbs.core.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.bbs.core.model.Module;
import com.bbs.core.model.Post;
import com.bbs.core.model.Topic;
import com.bbs.core.model.User;
import com.bbs.core.service.ModuleService;
import com.bbs.core.service.TopicService;
import com.bbs.core.service.UserService;
import com.bbs.core.dao.PostDao;
import com.bbs.core.dao.ReplyDao;
import com.bbs.core.dao.TopicDao;
import com.github.pagehelper.PageHelper;

@Service(value = "topicService")
public class TopicServiceImpl implements TopicService{
	@Autowired
    private TopicDao topicDao;
	@Autowired
    private PostDao postDao;
	@Autowired
    private ReplyDao replyDao;
	@Autowired
    private UserService userService;
	
	@Autowired
    private ModuleService moduleService;
	
	@Override
	public Topic getTopicByPrimaryKey(int id) {
		return topicDao.selectTopicByPrimaryKey(id);
	}
	
	@Override
	@Cacheable("TOPIC")
	public List<Topic> getAllTopics() {
		
		List<Topic> selectAllTopic = topicDao.selectAllTopics();
		return selectAllTopic;
	}
	@Override
//	@Cacheable("MY-MESSAGE")
	public List<Topic> queryMyMessageTopic(int userId){
		return topicDao.queryMyMessageTopic(userId);
	}
	@Override
//	@Cacheable("MY-MESSAGE-COUNT")
	public Integer queryMyMessageTopicCount(int userId){
		return topicDao.queryMyMessageTopicCount(userId);
	}
	
	@Override
	@CacheEvict(cacheNames="TOPIC", allEntries=true)
	public void saveTopic(Topic topic, Post post, User user) {
		topic.setUserId(user.getId());
		topic.setCreateTime(new Date());
		topicDao.insertTopic(topic);
		post.setUserId(user.getId());
		post.setTopicId(topic.getId());
		post.setCreateTime(new Date());
		postDao.insert(post);
		userService.addTopicScore(user.getId());
	}
	
	@Override
	@CacheEvict(cacheNames="TOPIC", allEntries=true)
	public void deleteTopic(int id) {
		topicDao.deleteTopicByPrimaryKey(id);
		postDao.deleteByTopicId(id);
		replyDao.deleteByTopicId(id);
	}
	@Override
	@CacheEvict(cacheNames="TOPIC", allEntries=true)
	public void updateTopic(Topic topic){
		topicDao.updateTopicByPrimaryKey(topic);
	}
	
	@Override
	public List<Topic> getHotTopics() {
		return topicDao.selectHotTopics();
	}

	@Override
	public List<Topic> getNiceTopics() {
		return topicDao.selectNiceTopics();
	}

	/*
	* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
	* 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
	* pageNum 开始页数
	* pageSize 每页显示的数据条数
	* */
	@Override
	public List<Topic> findTopicsByPage(int pageNum, int pageSize) {
		//将参数传给这个方法就可以实现物理分页了，非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<Topic> topics = topicDao.selectAllTopics();
        for(Topic topic : topics){
        	User user = userService.selectByPrimaryKey(topic.getUserId());
        	Module module = moduleService.selectByPrimaryKey(topic.getModuleId());
        	topic.setUser(user);
        	topic.setModule(module);
        }
        return topics;
	}

}
